package com.team.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.DocumentDao;
import com.team.entity.Document;
import com.team.entity.Project;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.StringUtil;
import com.team.util.TUtil;

public class DocShareServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_m");
		if("save".equals(method)){
			saveDoc(request,response);
		}else if("select".equals(method)){
			selectAllDocs(request,response);
			
		}else if("find".equals(method)){
			findDoc(request,response);
		}else if("update".equals(method)){
			updateDoc(request,response);
		}else {
			response.sendError(404,"request a error path");
			return ;
		}

		
	}
	private void updateDoc(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String docId = request.getParameter("id");
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		
		if(StringUtil.isEmpty(name)||StringUtil.isEmpty(content)){
			response.sendRedirect("editdoc.jsp?code=1001");
			return ;
		}
		int id = Integer.parseInt(docId);
		HttpSession session = request.getSession();
		Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
		DocumentDao dd = new DocumentDao();
		int num = dd.updatDocById(name,content,id);
		if(num > 0){
			System.out.println("资料更新成功！！");
			response.sendRedirect("docShare.jspx?_m=select&projId="+project.getId());
		}else {
			System.out.println("资料更新失败！！");
		}
	}
	private void findDoc(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String docId = request.getParameter("id");
		int id = Integer.parseInt(docId);
		
		DocumentDao dd = new DocumentDao();
		Document doc = dd.selectDocumentById(id);
		if(doc != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_EDITDOC, doc);
			response.sendRedirect("editdoc.jsp");
			return ;
		}else {
			System.out.println("资料查找失败！！");
		}
		
	}
	private void selectAllDocs(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String projId = request.getParameter("projId");
		
			int id = Integer.parseInt(projId);
			
			List<Document> list = new ArrayList<Document>();
			DocumentDao dd = new DocumentDao();
			list = dd.selectAllDocsByProjId(id);
			
			ServletContext application = getServletContext();
			application.setAttribute(Const.APPLICATION_LISTDOCS, list);
			
			response.sendRedirect("doc.jsp");
			return ;
		
	}
	private void saveDoc(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String title = request.getParameter("title");
		String context = request.getParameter("context");
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(Const.SESSION_USER);
		Project project = (Project)session.getAttribute(Const.SESSION_PROJ);
		
		Document doc = new Document();
		doc.setTitle(title);
		doc.setContext(context);
		doc.setCreateTime(TUtil.getNow());
		doc.setProjId(project.getId());
		doc.setUserId(user.getId());
		session.setAttribute(Const.SESSION_DOC, doc);
		
		DocumentDao dao = new DocumentDao();
		int num = dao.saveDocument(doc);
		if(num  > 0){
			response.sendRedirect("docShare.jspx?_m=select");
			return;
		}else{
			response.sendRedirect("doc.jsp");
			
		}
	}
}
